
package suncertify.shared;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.File;
/**
 *
 * @author jshah
 * A utility class to set properties in "suncertify.properties" file for properties related to Rmi Server
 */
public class ServerProperty {

    /**
     *
     */

    /**
     * P Instance of <code>Properties</code> class to set properties for "suncertify.properties"
     */
    private static final Properties P = new Properties();
    private static File propertiesFile;
    static{

        ServerProperty.propertiesFile = new File(DefaultConfig.PROPERTYFILE);
        if(!ServerProperty.propertiesFile.exists()){
            try {
                ServerProperty.propertiesFile.createNewFile();
  
            } catch (IOException ex) {
                Logger.getLogger(ServerProperty.class.getName()).log(Level.SEVERE, "ERROR CREATING PROPERTIES FILE", ex);
            }
        }
        try {
            P.load(new FileInputStream(ServerProperty.propertiesFile));
        } catch (IOException ex) {
          
                Logger.getLogger(ServerProperty.class.getName()).log(Level.SEVERE, "ERROR LOADING PROPERTIES FILE Static INITIAlizer" , ex);
            
        }
    }
    private static final String SERVERDBFILE = "serverdbfile";

    private static final String CLIENTDBFILE = "clientdbfile";

    private static final String DEFAULTRMIURL = "defaultRmiUrl";

    private static final String DEFAULTRMIPORT = "defaultRmiPort";

    private static final String DEFAULRTRMIHOST = "defualtRmiHost";

    private static final String CLIENTRMIURL = "clientRmiUrl";

    private static final String CLIENTRMIPORT = "clientRmiPort";
    
    private static final String CLIENTRMIHOST = "clientRmiHost";
    /**
     * <code>Property</code> name for Rmi url to be used by Server
     */
    private static final String SERVERRMIURL = "serverRmiUrl";
    /**
     *  <code>Property</code> name for Rmi Port to be used by Server
     */
    private static final String SERVERRMIPORT = "serverRmiPort";
    /**
     *  <code>Property</code> name for Rmi Host to be used by Server
     */
    private static final String SERVERRMIHOST = "serverRmiHost";
    

    /**
     * Method to set the default properties for the RMI Server
     */
    public static void setDefaultProp(){
 

//        if(!(P.containsKey(ServerProperty.SERVERRMIHOST))|| (ServerProperty.getServerRmiHostProp()==null||ServerProperty.getServerRmiHostProp().isEmpty()))
//            P.setProperty(ServerProperty.SERVERRMIHOST, DefaultConfig.DEFAULTHOST);
//        if(!(P.containsKey(ServerProperty.SERVERRMIURL))|| (ServerProperty.getServerRmiUrlProp()==null||ServerProperty.getServerRmiUrlProp().isEmpty()))
//            P.setProperty(ServerProperty.SERVERRMIURL, DefaultConfig.DEFAULTURL);
//        if(!(P.containsKey(ServerProperty.SERVERRMIPORT))|| (ServerProperty.getServerRmiPortProp()==null||ServerProperty.getServerRmiPortProp().isEmpty()))
//            P.setProperty(ServerProperty.SERVERRMIPORT, DefaultConfig.DEFAULTPORT);
        
        P.setProperty(ServerProperty.DEFAULRTRMIHOST,DefaultConfig.DEFAULTHOST);
        P.setProperty(ServerProperty.DEFAULTRMIURL, DefaultConfig.DEFAULTURL);
        P.setProperty(ServerProperty.DEFAULTRMIPORT, DefaultConfig.DEFAULTPORT);

        ServerProperty.storeProps();

    }

    /**
     * @param host name of the rmi host (eg.localhost)
     * @param rmiurl the rmi url to locate the remote object
     * @param port the port at which the rmiserver is listening (default: 1099)
     * this method can be called once a valid set of Rmi parameters is entered by the user which can be stored
     * as default for next time if user wishes to connect to the same rmi server
     */
    public static void setReusableProp(String host, String rmiurl, String port){

        P.setProperty(ServerProperty.DEFAULRTRMIHOST,host);
        P.setProperty(ServerProperty.DEFAULTRMIURL, rmiurl);
        P.setProperty(ServerProperty.DEFAULTRMIPORT,port);
        ServerProperty.storeProps();

    }
    /**
     * set the value for <code>ServerProperty.SERVERRMIHOST</code>
     * @param propValue the value of the property to be set
     */
    public static void setServerRmiHostProp(String propValue){
        P.setProperty(ServerProperty.SERVERRMIHOST, propValue);
    }
    /**
     * set the value for <code>ServerProperty.SERVERRMIURL</code>
     * @param propValue the value of the property to be set
     */
    public static void setServerRmiUrlProp(String propValue){
        P.setProperty(ServerProperty.SERVERRMIURL, propValue);
    }
    /**
     * set the value for <code>ServerProperty.SERVERRMIPORT</code>
     * @param propValue the value of the property to be set
     */
    public static void setSeverRmiPortProp(String propValue){
        P.setProperty(ServerProperty.SERVERRMIPORT, propValue);
    }

    public static void setClientRmiPortProp(String propValue){
        P.setProperty(ServerProperty.CLIENTRMIPORT, propValue);
    }

    public static void setClientRmiHostProp(String propValue){
        P.setProperty(ServerProperty.CLIENTRMIHOST, propValue);
    }

    public static void setClientRmiUrlProp(String propValue){
        P.setProperty(ServerProperty.CLIENTRMIURL, propValue);
    }


    public static void setServerDBProp(String propValue){
        P.setProperty(ServerProperty.SERVERDBFILE,propValue);
    }

    public static void setClientDBProp(String propValue){
        P.setProperty(ServerProperty.CLIENTDBFILE, propValue);
    }

    /**
     *
     * @return  <code>ServerProperty.SERVERRMIHOST</code>
     */
    public static String getServerRmiHostProp(){
        return P.getProperty(ServerProperty.SERVERRMIHOST);
    }
    /**
     *
     * @return  <code>ServerProperty.SERVERRMIURL</code>
     */
    public static String getServerRmiUrlProp(){
        return P.getProperty(ServerProperty.SERVERRMIURL);
    }
    /**
     *
     * @return  <code>ServerProperty.SERVERRMIPORT</code>
     */
    public static String getServerRmiPortProp(){
        return P.getProperty(ServerProperty.SERVERRMIPORT);
    }

    public static String getDefaultRmiPortProp(){
        return P.getProperty(ServerProperty.DEFAULTRMIPORT);
    }

    public static String getDefaultRmiHostProp(){
        return P.getProperty(ServerProperty.DEFAULRTRMIHOST);
    }

    public static String getDefaultRmiUrlProp(){
        return P.getProperty(ServerProperty.DEFAULTRMIURL);
    }
    /**
     * Get the location of the properties file from <code>DefaultConfig.PROPERTYFILE</code> and write store the properties to this file
     */

    public static String getServerDBFileProp(){
        return P.getProperty(ServerProperty.SERVERDBFILE);
    }

    public static String getClientDBFileProp(){
        return P.getProperty(ServerProperty.CLIENTDBFILE);
    }

    public static File getPropFile(){
        return ServerProperty.propertiesFile;
    }

    /**
     * Write the properties to the properties file
     */
    public static void storeProps(){
        try {
                P.store(new FileOutputStream(ServerProperty.propertiesFile), "ISO 8859-1");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ServerProperty.class.getName()).log(Level.SEVERE, "PROPERTIES FILE NOT FOUND", ex);
        }catch(IOException io){
             Logger.getLogger(ServerProperty.class.getName()).log(Level.SEVERE, "ERROR OPENING PROPERTIES FILE", io);
        }
    }
}
